package com.wbao.dianniu.ui;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.customView.CustomDialog;
import com.wbao.dianniu.customView.LoadingWaitDialog;
import com.wbao.dianniu.data.RecruitAddData;
import com.wbao.dianniu.data.RecruitListData;
import com.wbao.dianniu.listener.IJobDeliveryAddListener;
import com.wbao.dianniu.listener.IRecruitDelListener;
import com.wbao.dianniu.listener.IRecruitDetailsListener;
import com.wbao.dianniu.listener.IRecruitStatusListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.JobDeliveryAddManager;
import com.wbao.dianniu.manager.RecruitDelManager;
import com.wbao.dianniu.manager.RecruitDetailsManager;
import com.wbao.dianniu.manager.RecruitListChangeManager;
import com.wbao.dianniu.manager.RecruitStatusManager;
import com.wbao.dianniu.manager.ShareSDKManager;
import com.wbao.dianniu.manager.RecruitListChangeManager.RecruitChangedListener;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.R;

/**
 * 岗位详情
 * 
 * @author yejianjun
 *
 */
public class RecruitDetailsActivity extends BaseActivity implements IRecruitDetailsListener,IJobDeliveryAddListener,IRecruitDelListener,IRecruitStatusListener,RecruitChangedListener{
	
	private LinearLayout waittingLayout;
	private ProgressBar waittingProBar;
	private TextView waittingTV;
	
	private RecruitDetailsManager manager;
	private JobDeliveryAddManager jdManager;
	private RecruitStatusManager statusManager;
	private RecruitDelManager delManger;
	private TextView tv_post;
	private TextView tv_salary;
	private TextView tv_tryout;
	private TextView tv_trial_ruel;
	private TextView tv_recruitCount;
	private TextView tv_address;
	private TextView tv_experience;
	private TextView tv_education;
	private TextView tv_describle;
	private int recruitId ;
	private Button applyButton;
	private Dialog pDialog;
	private Button rightBtn;
	private Dialog dialog;
	private RecruitAddData data;
	private Button deleteBtn;
	private Button editBtn;
	private Button closeBtn;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.activity_recruit_details, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		setTitleName(getResources().getString(R.string.position_details));
		initWattingView();
		init();
		manager = new RecruitDetailsManager(this);
		manager.addRecruitDetailsListener(this);
		recruitId = getIntent().getIntExtra(GlobalContext.INTENT_JOB_ID, 0);
		manager.recruitDetails(recruitId,GlobalContext.accountId);
		jdManager = new JobDeliveryAddManager(this);
		jdManager.addJobDeliveryAddListener(this);
		statusManager = new RecruitStatusManager(this);
		statusManager.addRecruitStatusListener(this);
		delManger = new RecruitDelManager(this);
		delManger.addRecruitDelListener(this);
		RecruitListChangeManager.getInstance().addRecruitChangeListener(this);
	}
	
	private void initWattingView(){
		waittingLayout = (LinearLayout) findViewById(R.id.waitting_layout);
		waittingProBar = (ProgressBar) findViewById(R.id.waitting_progress);
		waittingTV = (TextView) findViewById(R.id.waitting_text_desc);
		waittingLayout.setVisibility(View.VISIBLE);
	}
	
	private void init() {
		tv_post = (TextView) findViewById(R.id.recruit_detail_post);
		tv_tryout = (TextView) findViewById(R.id.recruit_detail_trial);
		tv_trial_ruel = (TextView) findViewById(R.id.recruit_detail_trial_ruel);
		tv_salary = (TextView) findViewById(R.id.recruit_detail_salary);
		tv_address = (TextView) findViewById(R.id.recruit_detail_address);
		tv_recruitCount = (TextView) findViewById(R.id.recruit_detail_recruitCount);
		tv_experience = (TextView) findViewById(R.id.recruit_detail_experience);
		tv_education = (TextView) findViewById(R.id.recruit_detail_education);
		tv_describle = (TextView) findViewById(R.id.recruit_detail_describe);
		Button btn_jubao = (Button) findViewById(R.id.recruit_detail_report);
		btn_jubao.setOnClickListener(this);
		applyButton = (Button) findViewById(R.id.recruit_detail_applay_job);
		closeBtn = (Button) findViewById(R.id.recruit_detail_close);
		editBtn = (Button) findViewById(R.id.recruit_detail_edit);
		deleteBtn = (Button) findViewById(R.id.recruit_detail_delete);
		applyButton.setOnClickListener(this);
		closeBtn.setOnClickListener(this);
		editBtn.setOnClickListener(this);
		deleteBtn.setOnClickListener(this);
		tv_trial_ruel.setOnClickListener(this);
		
		rightBtn = getRightTextButton();
		rightBtn.setText(getResources().getString(R.string.share));
		rightBtn.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.recruit_detail_report:	//举报
			Intent jbIntent = new Intent();
			//类型4 举报发布的招聘
			jbIntent.putExtra(GlobalContext.INTENT_REPORT_TYPE,4);
			jbIntent.putExtra(GlobalContext.INTENT_REPORT_OBJECTID, recruitId);
			jbIntent.setClass(RecruitDetailsActivity.this, ReportActivity.class);
			startActivity(jbIntent);
			break;
		case R.id.recruit_detail_applay_job:	//申请岗位
			if (!GlobalContext.isPerfectData) {
				showAuthLevel("您还未完善资料，请先完善资料！");
				return;
			}
			if (!GlobalContext.isPerfectResume) {
				showJobAuth("您还未填写简历，是否现在完善！");
				return;
			}
			pDialog = LoadingWaitDialog.createLoadingDialog(this, "");
			pDialog.show();
			jdManager.jobDeliveryAdd(GlobalContext.accountId, recruitId,data.getPost(), data.getAccountId());
			break;
		case R.id.recruit_detail_close:
			int st = 0;
			if (0 == data.getStatus()) {
				st = 1;
				showNotify(st, "当前已停止招聘，打开会启动招聘");
			} else {
				st = 0;
				showNotify(st, "当前正在招聘，关闭后会停止招聘");
			}
			break;
		case R.id.recruit_detail_edit:
			Intent editIntent = new Intent();
			Bundle bundle = new Bundle();
			bundle.putInt(GlobalContext.BUNDLE_ID, data.getId());
			bundle.putString(GlobalContext.BUNDLE_POST, tv_post.getText().toString().trim());
			bundle.putString(GlobalContext.BUNDLE_SALARY, tv_salary.getText().toString().trim());
			bundle.putString(GlobalContext.BUNDLE_TRYOUT, tv_tryout.getText().toString().trim());
			bundle.putString(GlobalContext.BUNDLE_COUNT, tv_recruitCount.getText().toString().trim() );
			bundle.putString(GlobalContext.BUNDLE_ADDRESS,tv_address.getText().toString().trim() );
			bundle.putString(GlobalContext.BUNDLE_EXPERIENCE, tv_experience.getText().toString().trim());
			bundle.putString(GlobalContext.BUNDLE_EDUCATION, tv_education.getText().toString().trim());
			bundle.putString(GlobalContext.BUNDLE_DESCRIBLE,tv_describle.getText().toString().trim() );
			editIntent.putExtra(GlobalContext.BUNDLE_EDIT, bundle);
			editIntent.setClass(this, RecruitReleaseActivity.class);
			startActivity(editIntent);
			break;
		case R.id.recruit_detail_delete:
			delDialog();
			break;
		case R.id.right_button:	//分享
			// 调用分享内容
			String	imageUrl = GlobalContext.cdndownUrl + "logo.png";
			String content = "职位：" + data.getPost() + "\n" + "薪资：" + data.getSalary();
			ShareSDKManager.getInstance().showShare(this, content, 5, imageUrl, data.getId());
			break;
		case R.id.recruit_detail_trial_ruel:	//试用规则
			Intent intent = new Intent();
			intent.setClass(RecruitDetailsActivity.this, TrialRuelActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
	}
	
	/**
	 * 显示认证弹出框
	 */
	private void showAuthLevel(String msg) {
		dialog = new Dialog(this, R.style.custom_Dialog);
		Window win = dialog.getWindow();
		win.getDecorView().setPadding(3, 3, 3, 3);
		win.setGravity(Gravity.CENTER);
		View view = LayoutInflater.from(this).inflate(R.layout.custom_message_dialog, null);
		Button okBtn = (Button) view.findViewById(R.id.custom_msg_ok);
		Button cancelBtn = (Button) view.findViewById(R.id.custom_msg_cancel);
		TextView message = (TextView) view.findViewById(R.id.custom_msg_message);
		message.setText(msg);
		dialog.setContentView(view);
		dialog.setCancelable(true);
		dialog.show();
		okBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (null != dialog && dialog.isShowing()) {
					dialog.dismiss();
				}
				// 跳转到认证界面
				Intent intent = new Intent();
				intent.setClass(RecruitDetailsActivity.this, PerfectInfoActivity.class);
				startActivity(intent);
			}
		});
		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null != dialog && dialog.isShowing()) {
					dialog.dismiss();
				}
			}
		});
	}
	/**
	 * 显示完善简历弹出框
	 */
	private void showJobAuth(String msg) {
		dialog = new Dialog(this, R.style.custom_Dialog);
		Window win = dialog.getWindow();
		win.getDecorView().setPadding(3, 3, 3, 3);
		win.setGravity(Gravity.CENTER);
		View view = LayoutInflater.from(this).inflate(R.layout.custom_message_dialog, null);
		Button okBtn = (Button) view.findViewById(R.id.custom_msg_ok);
		Button cancelBtn = (Button) view.findViewById(R.id.custom_msg_cancel);
		TextView message = (TextView) view.findViewById(R.id.custom_msg_message);
		message.setText(msg);
		dialog.setContentView(view);
		dialog.setCancelable(true);
		dialog.show();
		okBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (null != dialog && dialog.isShowing()) {
					dialog.dismiss();
				}
				// 跳转到编辑简历界面
				Intent intent = new Intent();
				intent.setClass(RecruitDetailsActivity.this, JobEditActivity.class);
				startActivity(intent);
			}
		});
		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null != dialog && dialog.isShowing()) {
					dialog.dismiss();
				}
			}
		});
	}
	/**
	 * 显示认证弹出框
	 */
//	private void showDialog() {
//		dialog = new Dialog(this, R.style.custom_Dialog);
//		Window win = dialog.getWindow();
//		win.getDecorView().setPadding(3, 3, 3, 3);
//		win.setGravity(Gravity.BOTTOM);
//		View view = LayoutInflater.from(this).inflate(R.layout.jobrecruit_addlist_dialog, null);
//		Button applyBtn = (Button) view.findViewById(R.id.apply_job_recruit_list);
//		Button cancelBtn = (Button) view.findViewById(R.id.cancel);
//		//申请用户列表
//		applyBtn.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				if(null != dialog && dialog.isShowing()){
//					dialog.dismiss();
//				}
//				Intent intent = new  Intent();
//				intent.putExtra(GlobalContext.INTENT_REPORT_OBJECTID, recruitId);
//				intent.putExtra(GlobalContext.INTENT_REPORT_TYPE, 1);
//				intent.setClass(RecruitDetailsActivity.this, JobRecruitAddListActivity.class);
//				startActivity(intent);	
//			}
//		});
//		
//		// 取消
//		cancelBtn.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				if(null != dialog && dialog.isShowing()){
//					dialog.dismiss();
//				}
//			}
//		});
//		dialog.setContentView(view);
//		dialog.setCancelable(true);
//		dialog.show();
//	}

	/**
	 * 删除求职对话框
	 */
	private void delDialog(){
		CustomDialog.Builder builder = new CustomDialog.Builder(this);  
		builder.setTitle("确认删除本条招聘吗？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
            public void onClick(DialogInterface dialog, int which) {  
                dialog.dismiss();  
                //设置你的操作事项  
                delManger.jobRecruitDel(GlobalContext.accountId, recruitId);
            }  
        });  
        builder.setNegativeButton("取消",  
                new android.content.DialogInterface.OnClickListener() {  
                    public void onClick(DialogInterface dialog, int which) {  
                        dialog.dismiss();  
                    }  
                });  
        builder.create().show();  
	}
	
	@Override
	public void onRecruitDetailsFailure(int errorCode, String errorMsg) {
		waittingLayout.setVisibility(View.VISIBLE);
		waittingProBar.setVisibility(View.GONE);
		waittingTV.setText("加载失败，请稍后再试...");
	}

	@Override
	public void onRecruitDetailsSuccess(RecruitAddData data) {
		if(null == data){
			waittingLayout.setVisibility(View.VISIBLE);
			waittingProBar.setVisibility(View.GONE);
			waittingTV.setText("数据为空...");
			return;
		}
		waittingLayout.setVisibility(View.GONE);
//		rightImageBtn.setVisibility(View.VISIBLE);
		this.data = data;
		display(data);
	}
	
	private void display(RecruitAddData data){
		tv_post.setText(data.getPost()== null ? "": data.getPost());
		tv_tryout.setText(data.getTryout() == null ? "": data.getTryout());
		tv_salary.setText(data.getSalary()== null ? "": data.getSalary());
		tv_address.setText(data.getAddress() == null ? "": data.getAddress());
		tv_recruitCount.setText(data.getRecruitCount() == null ? "": data.getRecruitCount());
		tv_experience.setText(data.getExperience() == null ? "" : data.getExperience());
		tv_education.setText(data.getEducation() == null ? "": data.getEducation());
		tv_describle.setText(data.getDescribe() == null ? "": data.getDescribe());
		if(data.getAccountId() == GlobalContext.accountId){
			applyButton.setVisibility(View.GONE);
			if( 1 ==data.getStatus()){
				closeBtn.setText("关闭");
			}else{
				closeBtn.setText("打开");
			}
		}else{
			applyButton.setVisibility(View.VISIBLE);
			if(0 == data.getDeliverStatus()){
				applyButton.setText(getResources().getString(R.string.apply_jobs));
				applyButton.setClickable(true);
			}else{
				applyButton.setText(getResources().getString(R.string.has_delivered));
				applyButton.setBackgroundColor(getResources().getColor(R.color.remark_gray_bg));
				applyButton.setClickable(false);
			}
		}
	}

	@Override
	public void onRecruitDelFailure(int errorCode, String errorMsg) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onRecruitDelSuccess() {
		RecruitListChangeManager.getInstance().notifyRemove(recruitId);
		Notification.toast(this, "已删除");
		finish();
	}

	@Override
	public void onRecruitStatusFailure(int errorCode, String errorMsg) {
		
	}

	@Override
	public void onRecruitStatusSuccess() {
		if( 0 ==data.getStatus()){
			data.setStatus(1);
			closeBtn.setText("关闭");
		}else{
			data.setStatus(0);
			closeBtn.setText("打开");
		}
		
	}

	@Override
	public void onJobDeliveryAddFailure(int errorCode, String errorMsg) {
		if(null != pDialog && pDialog.isShowing()){
			pDialog.dismiss();
		}
	}

	@Override
	public void onJobDeliveryAddSuccess() {
		// TODO Auto-generated method stub
		if(null != pDialog && pDialog.isShowing()){
			pDialog.dismiss();
		}
		applyButton.setText(getResources().getString(R.string.has_delivered));
		applyButton.setBackgroundColor(getResources().getColor(R.color.remark_gray_bg));
		applyButton.setClickable(false);
	}

	@Override
	public void recruitAddChanged(RecruitListData data) {
		
	}

	@Override
	public void recruitRemoveChanged(int position) {
		
	}

	@Override
	public void recruitModifyChanged(int id, RecruitAddData data) {
		display(data);
	}
	
	private void showNotify(final int status , String msg){
		CustomDialog.Builder builder = new CustomDialog.Builder(this);
		builder.setTitle(msg);
		builder.setPositiveButton(getResources().getString(R.string.confirm),
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						statusManager.recruitStatus(GlobalContext.accountId, recruitId, status);
					}
				});
		builder.setNegativeButton(getResources().getString(R.string.cancel),
				new android.content.DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();
	}
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		}
		public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		}
}
